<template>
  <div class="dashboard-container">
    <!-- 表格 -->
    <el-table
      :data="list"
      border
      fit
      highlight-current-row>
      <el-table-column
        label="序号"
        width="70"
        align="center">
        <template slot-scope="scope">
          {{ (page - 1) * limit + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column prop="title" label="标题" width="80" />
      <el-table-column prop="imageUrl" label="图片地址" />
      <el-table-column prop="linkUrl" label="链接地址" width="100"/>
      <el-table-column prop="gmtCreate" label="添加时间" width="160"/>
      <el-table-column prop="sort" label="排序" width="60" />
      <el-table-column label="操作" width="200" align="center">
        <template slot-scope="scope">
          <router-link :to="'/banner/edit/'+scope.row.id">
            <el-button type="primary" size="mini" icon="el-icon-edit">修改</el-button>
          </router-link>
          <el-button type="danger" size="mini" icon="el-icon-delete" @click="removeDataById(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <el-pagination
      :current-page="page"
      :page-size="limit"
      :total="total"
      style="padding: 30px 0; text-align: center;"
      layout="total, prev, pager, next, jumper"
      @current-change="getList"
    />
  </div>
</template>
<script>
import banner from '@/api/edu/banner'

export default {
    data() { // 定义变量和初始值
        return {
            list: null,
            page: 1,
            limit: 10,
            total: 0
        }
    },
    created() { // 页面渲染之前执行，一般调用methods中定义的方法
        this.getList()
    },
    methods: { // 创建具体的方法，调用teacher.js中定义的方法
        getList(page = 1) {
            this.page = page
            banner.getBannerListPage(this.page, this.limit).then(response => { // 请求成功
                this.list = response.data.rows
                this.total = response.data.total
                console.log(this.list)
                console.log(this.total)
            })
        },
        resetData() { // 表单清空的方法
            this.getList()
        },
        removeDataById(id) {
            this.$confirm('此操作会永久删除banner，是否继续？', '提示', {
                confirmButtonText: '确认',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                banner.deleteBannerById(id).then(response => {
                    this.$message({
                        type: 'success',
                        message: '删除banner成功！'
                    })
                    this.getList()
                })
            })
        }
    }
}
</script>
